package com.yanqu.road.server.gameplayer.module.wechatrank;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.wechatrank.eWechatRankSwitchStatusType;
import com.yanqu.road.pb.WechatRankBlackUserProto.WechatRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.WechatRankBlackUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ServerChannel;

/**
 * 微信排行
 */
public class WechatRankModule extends GeneralModule {

    public WechatRankModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        this.syncIsPostWechatRankMsg();
    }

    /**
     * 同步信息
     */
    private void syncIsPostWechatRankMsg() {
        //获取是否能上报
        boolean flag = this.getIsPostWechatRankFlag();
        //获取开关状态
        int status = (int)player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.WechatRankPostSwitchStatus);
        WechatRankProto.WechatRankIsPostSyncMsg.Builder builder = WechatRankProto.WechatRankIsPostSyncMsg.newBuilder();
        builder.setFlag(flag);
        builder.setSwitchStatus(status);
        player.sendPacket(Protocol.U_WECHAT_RANK_IS_POST_MSG, builder);
    }

    /**
     * 获取是否能上报微信状态
     * @return
     */
    private boolean getIsPostWechatRankFlag() {
        boolean flag = false;
        //获取开关状态
        int status = (int)player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.WechatRankPostSwitchStatus);
        //判断玩家是否是黑名单
        boolean blackFlag = WechatRankBlackUserMgr.isWechatRankBlackUser(player.getUserInfo().getOpenId());
        //玩家属于201渠道
        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        boolean channelFlag = channelId == eChannelType.XiaoChengXu.getValue() || channelId == eChannelType.MainLandInnerTest.getValue();
        //不在黑名单 && 开关开着 && 属于微信小程序渠道
        if (!blackFlag && status == eWechatRankSwitchStatusType.SWITCH_STATUS_TYPE_OPEN.getType() && channelFlag) {
            flag = true;
        }
        return flag;
    }

    /**
     * 操作开关
     */
    public void operateSwitch() {
        //获取开关状态
        int status = (int)player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.WechatRankPostSwitchStatus);
        if (status == eWechatRankSwitchStatusType.SWITCH_STATUS_TYPE_OPEN.getType()) {
            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.WechatRankPostSwitchStatus, eWechatRankSwitchStatusType.SWITCH_STATUS_TYPE_CLOSE.getType());
        } else if (status == eWechatRankSwitchStatusType.SWITCH_STATUS_TYPE_CLOSE.getType()) {
            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.WechatRankPostSwitchStatus, eWechatRankSwitchStatusType.SWITCH_STATUS_TYPE_OPEN.getType());
        }
        WechatRankProto.WechatRankOperateSwitchRespMsg.Builder respMsg = WechatRankProto.WechatRankOperateSwitchRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setStatus((int)player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.WechatRankPostSwitchStatus));
        respMsg.setFlag(this.getIsPostWechatRankFlag());
        player.sendPacket(Protocol.U_WECHAT_RANK_OPERATE_POST_SWITCH, respMsg);
    }
}
